/*
 * $Id: OSInfo.java 36 2012-01-25 14:07:21Z chr.grote@gmail.com $ Created on 02.10.2004
 */
package net.sourceforge.ftgl.util.loader;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * TODO javadoc
 * 
 * @author root
 */
public class OSInfo
{
	private final static Map<String, Object> NAME_ALIAS;
	private final static Map<String, Object> ARCH_ALIAS;

	// for more names see http://lopica.sourceforge.net/os.html

	/** string idicating Windows OS */
	public static final String	OS_WIN		= "windows";
	/** string idicating Mac OS >= version 10 */
	public static final String	OS_MACOSX	= "macosx";
	/** string idicating Solaris */
	public static final String	OS_SOLARIS	= "solaris";

	static
	{
		final HashMap<String, Object> arch = new HashMap<String, Object>();
		arch.put("ppc", "power pc");
		arch.put("x86", "i\\d86");
		arch.put("x86_64", "amd64");
		arch.put("pa_risk", "pa-risk");

		final HashMap<String, Object> name = new HashMap<String, Object>();
		name.put(OS_WIN, "windows.*");
		name.put(OS_MACOSX, "mac os x");
		name.put(OS_SOLARIS, "sunos");

		compileValues(name);
		compileValues(arch);

		NAME_ALIAS = Collections.unmodifiableMap(name);
		ARCH_ALIAS = Collections.unmodifiableMap(arch);
	}

	private static void compileValues(Map<String, Object> m)
	{
		final Iterator<Map.Entry<String, Object>> i = m.entrySet().iterator();
		while (i.hasNext())
		{
			final Map.Entry<String, Object> e = i.next();
			final String k = (String)e.getValue();
			e.setValue(Pattern.compile(k));
		}
	}

	private static String alias(Map<String, Object> m, String v)
	{
		v = v.toLowerCase();
		final Iterator<Map.Entry<String, Object>> i = m.entrySet().iterator();
		while (i.hasNext())
		{
			final Map.Entry<String, Object> e = i.next();
			final Pattern p = (Pattern)e.getValue();
			if (p.matcher(v).matches())
				return e.getKey();
		}
		return v;
	}

	private static String getDefaultName()
	{
		return System.getProperty("os.name");
	}

	private static String getDefaultArch()
	{
		return System.getProperty("os.arch");
	}

	private static int getDefaultModel()
	{
		try
		{
			return Integer.parseInt(System.getProperty("sun.arch.data.model", "0"));
		}
		catch (final NumberFormatException e)
		{
			return 0;
		}
	}

	private final String	name;
	private final String	arch;

	/**
	 * Creates OSInfo-Object with Infos about the current System according to the System-properties.
	 */
	public OSInfo()
	{
		this(getDefaultName(), getDefaultArch(), getDefaultModel());
	}

	/**
	 * TODO_IO javadoc
	 * 
	 * @param name
	 * @param arch
	 * @param datamodel
	 */
	public OSInfo(String name, String arch, int datamodel)
	{
		name = alias(NAME_ALIAS, name);
		arch = alias(ARCH_ALIAS, arch);

		if (arch.equals("ppc") && datamodel > 32)
			arch = arch + datamodel;

		this.name = name;
		this.arch = arch;
	}

	/**
	 * Get the name of the OS.
	 * 
	 * @return the name of the OS
	 */
	public String getOSName()
	{
		return this.name;
	}

	/**
	 * Get the name of the architecture.
	 * 
	 * @return the name of the architecture
	 */
	public String getArchName()
	{
		return this.arch;
	}
}